<?php

// | Copyright (c) 2019~2020 http://jiunidu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: huanglijun <80792275@qq.com>
// +----------------------------------------------------------------------

namespace service\Pay;

use think\facade\Log;
use think\Db;
 
class PayDb 
{
    protected $user_table           =   'user';
    protected $pay_table            =   'pay';
    protected $refund_table         =   'refund';
    protected $error = null;

    public function __construct()
    {
        //parent::__construct();
        Db::listen(function($sql, $time, $explain){
            // 记录SQL
            Log::write($sql. ' ['.$time.'s]',"sql");
            // 查看性能分析结果
            //dump($explain);
        });
    }
    /**
     *  添加或者更新一条支付
     */
    public function update_pay_data($data=[])
    {
        $insert_id=-1;
        if(empty($data['order_id']))
        {
            return $insert_id;
        }
        $pay_info=$this->get_pay_info($data['order_id'],'pay_id');
        //不存在是添加
        if(empty($pay_info))
        {
            $insert_num=Db::name($this->pay_table)->insert($data);                                             
            if((int)$insert_num >0 )
            {
                $insert_id=Db::name($this->pay_table)->getLastInsID();
                return $insert_id;            
            }
            return $insert_id;
        }
        else
        {
            $update_data['update_time']=time();
            $prepay_id=empty($data['prepay_id'])?'':$data['prepay_id'];
            $update_data['prepay_id']=$prepay_id;
            $update_num=Db::name($this->pay_table)->where('order_id', $data['order_id'])->update($update_data);
            if((int)$update_num>0)
            {
                return $update_num;
            }
        }
        return $insert_id;
    }
     /**
     *  更新支付状态信息
     */
    public function update_paystatus_data($order_id='',$data=[])
    {
        $update_num=-1;
        $update_data['update_time']=time();
        $update_data['transaction_id']=empty($data['transaction_id'])?'':$data['transaction_id'];
        $update_data['payed']=$data['payed'];
        $update_num=Db::name($this->pay_table)->where('order_id', $order_id)->update($update_data);
        if((int)$update_num>0)
        {
            return $update_num;
        }
    }
    /**
     *  更新退款状态信息
     */
    public function update_refundstatus_data($order_id='',$refund_id='',$data=[])
    {
        $update_num=-1;
        $update_data['update_time']=time();
        $update_data['transaction_id']=empty($data['transaction_id'])?'':$data['transaction_id'];
        $update_data['refunded']=$data['refunded'];
        $where=[['order_id','=', $order_id],['refund_id','=', $refund_id]];
        $update_num=Db::name($this->refund_table)->where($where)->update($update_data);
        if((int)$update_num>0)
        {
            return $update_num;
        }
    }
    /**
     *  添加或者更新一条退款记录
     */
    public function update_refund_data($data=[])
    {
        $insert_id=-1;
        if(empty($data['order_id']) && empty($data['refund_id']))
        {
            return $insert_id;
        }
        $refund_info=$this->get_refund_info($data['order_id'],$data['refund_id'],'id');
        //不存在是添加
        if(empty($refund_info))
        {
            $insert_num=Db::name($this->refund_table)->insert($data);                                             
            if((int)$insert_num >0 )
            {
                $insert_id=Db::name($this->refund_table)->getLastInsID();
                return $insert_id;            
            }
            return $insert_id;
        }
        else
        {
            $update_data['update_time']=time();
            $update_where=[['order_id','=',$data['order_id']],['refund_id','=',$data['refund_id']]];
               $update_num=Db::name($this->refund_table)->where( $update_where)->update($update_data);
               
            if((int)$update_num>0)
            {
                //var_dump('update_num:' . $update_num);
                return $update_num;
            }
        }
         //var_dump('insert_id: ' . $insert_id);
        return $insert_id;
    }
    /**
     *  获取用户信息
     */
    public function get_user_info($user_id=0,$field="")
    {
        $return_array=[];
        if(empty($user_id)||empty($field))
        {
            return $return_array;
        }
        $where=[['user_id','=',$user_id],['user_yesno','=',1]];
        $return_array=Db::name($this->user_table)
                            ->field($field)
                            ->where($where)
                            ->find();
        return $return_array;    
    }
    /**
     *  获取支付信息
     */
    public function get_pay_info($order_id=0,$field="")
    {
        $return_array=[];
        if(empty($order_id)||empty($field))
        {
            return $return_array;
        }
        $where=[['order_id','=',$order_id]];
        $return_array=Db::name($this->pay_table)
                            ->field($field)
                            ->where($where)
                            ->find();
        return $return_array;               
    }
    /**
     *  获取退款信息
     */
    public function get_refund_info($order_id=0,$refund_id=0,$field="")
    {
        $return_array=[];
        if(empty($order_id)||empty($field))
        {
            return $return_array;
        }
        $where=[['order_id','=',$order_id],['refund_id','=',$refund_id]];
        $return_array=Db::name($this->refund_table)
                            ->field($field)
                            ->where($where)
                            ->find();
        return $return_array;               
    }
}